package ExchangeSort;/*
 *
 *@author:    Xavier
 *@data:      2023 - 08 - 27
 *@time:      17:54
 *
 */

import java.util.Arrays;

//冒泡排序

/*
 * 时间复杂度：O(N^2) 对数据不敏感 不管有序无序都是这个
 * 空间复杂度：O(1)
 * 稳定性；稳定
 *  插入 冒泡 是稳定的
 * */
public class BubbleSort {
    public static void bubbleSort(int[] arr){
        for (int i = 0; i < arr.length-1; i++) {
            boolean flg = false;
            for (int j = 0; j < arr.length-1-i; j++) {
                if (arr[j] > arr[j+1]) {
                    swap(arr,j,j+1);
                    flg = true;
                }
            }
            if (!flg) {
                break;
            }
        }
    }
    private static void swap(int[] arr, int i , int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    public static void main(String[] args) {
        int[] arr1 = {2,8,7,3,9,5,9,0,0,2};
        int[] arr = {10,2,8,7,3,9,5,9,0,0,2};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
    }
}
